package ${package.Controller};

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import lombok.RequiredArgsConstructor;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.yunc.base.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};

import java.util.List;

/**
 * ${table.comment!} 前端控制器
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("/<#if cfg.splitArgs??><#list cfg.splitArgs as splitArg><#if splitArg_index = 0>${splitArg}<#else>/${splitArg}</#if></#list></#if>")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    private final ${table.serviceName} ${cfg.entityName}Service;

    @GetMapping("/page")
    <#if cfg.splitArgs??>
    @PreAuthorize("@ss.hasPermi('<#list cfg.splitArgs as splitArg><#if splitArg_index = 0>${splitArg}<#else>:${splitArg}</#if></#list>:list')")
    </#if>
    public R page(Page<${table.entityName}> page, ${table.entityName} ${cfg.entityName}) {
        QueryWrapper<${table.entityName}> qw = new QueryWrapper<>();
        <#if cfg.searchList??>
        <#list cfg.searchList as search>
        if (StringUtils.isNotEmpty(${cfg.entityName}.get${search.upperCaseColumnName}())) {
        <#if search.searchType == '1'>
            qw.eq("${search.sqlColumnName}", ${cfg.entityName}.get${search.upperCaseColumnName}());
        <#elseif search.searchType == '2'>
            qw.like("${search.sqlColumnName}", ${cfg.entityName}.get${search.upperCaseColumnName}());
        </#if>
        }
        </#list>
        </#if>
        <#list table.fields as field><#if field.propertyName == 'orderNum'>qw.orderByAsc("order_num");
            <#elseif field.propertyName == 'createTime'>qw.orderByDesc("create_time");
            </#if>
        </#list>
        ${cfg.entityName}Service.page(page, qw);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/info/{id}")
    <#if cfg.splitArgs??>
    @PreAuthorize("@ss.hasPermi('<#list cfg.splitArgs as splitArg><#if splitArg_index = 0>${splitArg}<#else>:${splitArg}</#if></#list>:list')")
    </#if>
    public R info(@PathVariable Long id) {
        ${table.entityName} ${cfg.entityName} = ${cfg.entityName}Service.getById(id);
        return R.ok().put("${cfg.entityName}", ${cfg.entityName});
    }

    @PostMapping("/save")
    <#if cfg.splitArgs??>
    @PreAuthorize("@ss.hasPermi('<#list cfg.splitArgs as splitArg><#if splitArg_index = 0>${splitArg}<#else>:${splitArg}</#if></#list>:save')")
    </#if>
    public R save(${table.entityName} ${cfg.entityName}) {
        boolean flag = ${cfg.entityName}Service.save(${cfg.entityName});
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping("/update")
    <#if cfg.splitArgs??>
    @PreAuthorize("@ss.hasPermi('<#list cfg.splitArgs as splitArg><#if splitArg_index = 0>${splitArg}<#else>:${splitArg}</#if></#list>:update')")
    </#if>
    public R update(${table.entityName} ${cfg.entityName}) {
        boolean flag = ${cfg.entityName}Service.updateById(${cfg.entityName});
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/delete/{ids}")
    <#if cfg.splitArgs??>
    @PreAuthorize("@ss.hasPermi('<#list cfg.splitArgs as splitArg><#if splitArg_index = 0>${splitArg}<#else>:${splitArg}</#if></#list>:delete')")
    </#if>
    public R delete(@PathVariable String ids) {
        List<String> idsList = StrSpliter.splitTrim(ids, ',', true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = ${cfg.entityName}Service.removeByIds(idsList);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}